package si.cir.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import si.cir.entities.IzpitniRok;

public class AipsHandler extends DefaultHandler{
	
	private List<IzpitniRok> izpitniRoki = new ArrayList<IzpitniRok>();
	private IzpitniRok izpitniRok;
	private String currentElement = "";
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.equals("izpitniRok"))
			izpitniRok = new IzpitniRok();
		else if(qName.equals("predmet"))
			currentElement = qName;
		else if(qName.equals("zavod"))
			currentElement = qName;
		else if(qName.equals("tip"))
			currentElement = qName;
		else if(qName.equals("komisijski"))
			currentElement = qName;
		else if(qName.equals("datum"))
			currentElement = qName;
		else if(qName.equals("ura"))
			currentElement = qName;
		else if(qName.equals("prostor"))
			currentElement = qName;
		else if(qName.equals("opomba"))
			currentElement = qName;
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("izpitniRok"))
			izpitniRoki.add(izpitniRok);
		else if(qName.equals("predmet"))
			currentElement = "";
		else if(qName.equals("zavod"))
			currentElement = "";
		else if(qName.equals("tip"))
			currentElement = "";
		else if(qName.equals("komisijski"))
			currentElement = "";
		else if(qName.equals("datum"))
			currentElement = "";
		else if(qName.equals("ura"))
			currentElement = "";
		else if(qName.equals("prostor"))
			currentElement = "";
		else if(qName.equals("opomba"))
			currentElement = "";
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(currentElement.equals("predmet"))
			izpitniRok.setPredmet(new String(ch, start, length));
		else if(currentElement.equals("zavod"))
			izpitniRok.setZavod(new String(ch, start, length));
		else if(currentElement.equals("tip"))
			izpitniRok.setTip(new String(ch, start, length));
		else if(currentElement.equals("komisijski"))
			izpitniRok.setKomisijski(new String(ch, start, length));
		else if(currentElement.equals("datum"))
			izpitniRok.setDatum(new String(ch, start, length));
		else if(currentElement.equals("ura"))
			izpitniRok.setUra(new String(ch, start, length));
		else if(currentElement.equals("prostor"))
			izpitniRok.setProstor(new String(ch, start, length));
		else if(currentElement.equals("opomba"))
			izpitniRok.setOpomba(new String(ch, start, length));
	}
	
	public List<IzpitniRok> getRoki(){
		return izpitniRoki;
	}
}

